# -*- encoding:utf8 -*-
from django.db import models
from staff.models import StaffInfo
from convert import str2dt, t2i

# 自定义库
from base_tools.base_model import BaseModel
# from django.dispatch import Signal
# read_avai_leave=Signal(providing_args=["instance"])

class AttendRecord(models.Model):
    staff = models.ForeignKey(StaffInfo, verbose_name=u'员工基本信息', db_column='staff_info', null=True, blank=True)
    date = models.CharField(u'日期', db_column='date', max_length=100, blank=True)
    enter_time = models.CharField(u'第一次打卡', db_column='workstart', max_length=100, blank=True)
    leave_time = models.CharField(u'最后一次打卡', db_column='workleave', max_length=100, blank=True)
    workspan = models.CharField(u'工作时长', db_column='workspan', max_length=100, blank=True)
    late_level = models.CharField(u'迟到等级', db_column='late_level', max_length=100, blank=True)
    late_person = models.CharField(u'个人迟到时间', db_column='late_person', max_length=100, blank=True)
    late_team = models.CharField(u'团队迟到时间', db_column='late_team', max_length=100, blank=True)
    overtime = models.CharField(u'加班时间', db_column='overtime', max_length=100, blank=True)
    absent = models.CharField(u'旷工时间', db_column='absent', max_length=100, blank=True)
    early_leave = models.CharField(u'早退时间', db_column='early_leave', max_length=100, blank=True)
    timestr = models.CharField(u'原始打卡记录', db_column='timestr', max_length=500, blank=True)
    note=models.TextField(verbose_name=u'备注', db_column='note', blank=True)

    class Meta:
        verbose_name = u'考勤记录'
        db_table = 'attend_record'


class AttendReport(models.Model):
    staff = models.ForeignKey(StaffInfo, verbose_name=u'员工基本信息', db_column='staff_info', null=True, blank=True)
    start_date = models.CharField(u'统计开始日期', db_column='startdate', max_length=100, blank=True)
    due_date = models.CharField(verbose_name=u'截止日期', db_column='duedate', max_length=100, blank=True)
    person_leave = models.CharField(u'事假统计', db_column='person_leave', max_length=100, blank=True)
    sick = models.CharField(u'病假统计', db_column='sick', max_length=100, blank=True)
    annual = models.CharField(u'年假统计', db_column='annual', max_length=100, blank=True)
    other_paid_leave = models.CharField(u'其他带薪假', db_column='other_paid_leave', max_length=100, blank=True)
    miss_check=models.CharField(u'漏打卡次数',db_column='miss_check', max_length=100, blank=True)
    swap_off = models.CharField(u'调休', db_column='swap_off', max_length=100, blank=True)
    paid_leave = models.CharField(u'带薪假', db_column='paid_leave', max_length=100, blank=True)
    late1 = models.CharField(u'迟到1统计', db_column='late1', max_length=100, blank=True)
    late2 = models.CharField(u'迟到2统计', db_column='late2', max_length=100, blank=True)
    late3 = models.CharField(u'迟到3统计', db_column='late3', max_length=100, blank=True)
    late4 = models.CharField(u'迟到4统计', db_column='late4', max_length=100, blank=True)
    late_person = models.CharField(u'个人迟到时间统计', db_column='late_person', max_length=100, blank=True)
    late_team = models.CharField(u'团队迟到时间统计', db_column='late_team', max_length=100, blank=True)
    overtime = models.CharField(u'加班时长', db_column='overtime', max_length=100, blank=True)
    exp_workday = models.CharField(u'应该工作天数', db_column='exp_workday', max_length=100, blank=True)
    act_workday = models.CharField(u'实际工作天数', db_column='act_workday', max_length=100, blank=True)
    allow_day = models.CharField(u'补贴天数', db_column='allow_day', max_length=100, blank=True)
    leave_day = models.CharField(u'总的请假天数', db_column='leave_day', max_length=100, blank=True)
    deduction_day = models.CharField(u'扣除工资天数', db_column='deduction_day', max_length=100, blank=True)
    absent = models.CharField(u'旷工时长', db_column='absent', max_length=100, blank=True)
    early_leave = models.CharField(u'早退时长', db_column='early_leave', max_length=100, blank=True)
    full_attend = models.CharField(u'是否全勤', db_column='full_attend', max_length=50, blank=True)
    process = models.CharField(u'警告类型', db_column='process', max_length=100, blank=True)
    note=models.TextField(verbose_name=u'备注', db_column='note', blank=True)

    class Meta:
        verbose_name = u'考勤统计'
        db_table = 'attend_report'


class AvaiLeave(models.Model):
    staff = models.ForeignKey(StaffInfo, verbose_name=u'员工基本信息', db_column='staff_info', null=True, blank=True)
    category = models.CharField(u'可用假期类型', db_column='category', max_length=100, blank=True)
    span = models.CharField(u'挣得的假期长度', db_column='span', max_length=100, blank=True)
    used = models.CharField(u'已使用假期长度', db_column='used', max_length=100, blank=True)
    start_date = models.CharField(u'假期产生日期', db_column='start', max_length=100, blank=True)
    is_valid = models.BooleanField(verbose_name=u'是否有效', db_column='is_valid', default=True)
    note=models.TextField(verbose_name=u'备注', db_column='note', blank=True)
    
    # def __init__(self,*args,**kw):
        # super(AvaiLeave,self).__init__(*args,**kw)
        # # 发出读取信号，处理该信号，可以用于更新年假等
        # read_avai_leave.send(sender=AvaiLeave,instance=self)
    
    class Meta:
        verbose_name = u'可用假期'
        db_table = 'avi_leave'

    def left(self):
        if not self.is_valid:
            return 0
        else:
            return t2i(self.span) - t2i(self.used)

    def __unicode__(self):
        return self.staff.cn_name + '_' + self.category + '_' + self.start_date
    
     
class LeaveApplication(BaseModel):
    """
    AttendIgnoreTimeSpanApplication
    
    考勤忽略时间段
    
    category:
    ================
    年假:annul
    调休:swap_off
    事假:personal
    病假:sick
    
    领导审批:
    ================
    pending: 刚提交，等待审批
    approved: 领导同意
    rejected: 领导拒绝
    
    super_leader
    ================
    事假超过两天需要echo审批，所以有super_leader_check字段，该字段取值多了一个状态:
    auto_approved: 指不需要echo审批，此字段默认值
    
    status
    ================
    pending: 等待
    checking
    approved
    rejected
    revoked:无效，员工取消，或者Hr取消等等情况
    
    """
    staff = models.ForeignKey(StaffInfo, verbose_name=u'员工基本信息', db_column='staff_info', null=True, blank=True)
    category = models.CharField(u'请假类型', db_column='category', max_length=100, blank=True)
    start_time = models.CharField(u'请假开始时间', db_column='start', max_length=100, blank=True)
    end_time = models.CharField(u'请假结束时间', db_column='end', max_length=100, blank=True)
    reason = models.TextField(u'请假原因', blank=True)   
    check_record=models.TextField(u'审批记录',db_column='check_record',blank=True)
    status = models.CharField(u'申请假期项的状态', db_column='status', max_length=100, blank=True)
    note=models.TextField(verbose_name=u'备注', db_column='note', blank=True)
    ope_history=models.TextField(u'操作记录',db_column='ope_history',blank=True)
    
    
    def __init__(self,*args,**kw):
        super(LeaveApplication,self).__init__(*args,**kw)
        self.org_status=self.status 
    
    class Meta:
        verbose_name = u'提交的请假'
        db_table = 'attend_ignore_application'

    def __unicode__(self):
        return self.staff.cn_name + '_' + self.category + '_' + self.start_time + '_' + self.end_time
        
        
class LeaveModel(models.Model):
    staff = models.ForeignKey(StaffInfo, verbose_name=u'员工基本信息', db_column='staff_info', null=True, blank=True)
    category = models.CharField(u'已用假期类型', db_column='category', max_length=100, blank=True)
    start_time = models.CharField(u'请假开始时间', db_column='start', max_length=100, blank=True)
    end_time = models.CharField(u'请假结束时间', db_column='end', max_length=100, blank=True)
    matched = models.BooleanField(u'请假是否匹配可用假期', db_column='matched', default=False, blank=True)
    submited_leave = models.ForeignKey(LeaveApplication, verbose_name=u'相关的假期申请', db_column='submited_leave', null=True,
                                       blank=True,related_name='usedleave')


    class Meta:
        verbose_name = u'已使用假期'
        db_table = 'attend_ignore'

    def span(self):
        _span = str2dt(self.end_time) - str2dt(self.start_time)
        return _span.seconds / 60

    def __unicode__(self):
        return self.staff.cn_name + '_' + self.category + '_' + self.start_time


class ConsumeRelation(models.Model):
    avai_leave = models.ForeignKey(AvaiLeave, verbose_name=u'对应的可用假期', db_column='avai_leave', null=True, blank=True,
                                   related_name='relation')
    avai_leave_start = models.CharField(u'对应可用假期的开始', db_column='avai_leave_start', max_length=100, blank=True)
    avai_leave_end = models.CharField(u'对应可用假期的结束', db_column='avai_leave_end', max_length=100, blank=True)
    used_leave = models.ForeignKey(LeaveModel, verbose_name=u'对应的已用假期', db_column='used_leave', null=True, blank=True,
                                   related_name='relation')
    used_leave_start = models.CharField(u'对应已用假期的开始时间', db_column='used_leave_start', max_length=100, blank=True)
    used_leave_end = models.CharField(u'对应已用假期的结束时间', db_column='used_leave_end', max_length=100, blank=True)

    class Meta:
        verbose_name = u'可用假期_已用假期_对应关系'
        db_table = 'consume_relation'

    def span(self):
        return t2i(self.avai_leave_end)-t2i(self.avai_leave_start)
    
    



class MsgModel(models.Model):
    """
    status:
    ==============
    pending
    
    假期审批种类:
    =================
    leave_check_pro
    leave_check_func
    leave_check_super
    """
    staff = models.ForeignKey(StaffInfo, verbose_name=u'接收员工', db_column='staff_info', null=True, blank=True)
    category = models.CharField(u'消息类型', db_column='category', max_length=200, blank=True)
    title=models.CharField(u'消息标题', db_column='title', max_length=500, blank=True)
    content = models.TextField(u'消息内容', db_column='content', blank=True)
    status = models.CharField(u'消息状态', db_column='status', max_length=100, blank=True, default='pending')
    gen_time = models.CharField(u'生成时间', db_column='gen_time', max_length=100, blank=True)
    note=models.TextField(verbose_name=u'备注', db_column='note', blank=True)

    class Meta:
        verbose_name = u'消息数据'
        db_table = 'msg_model'


class SpecialWorkshift(BaseModel):
    u"""
    默认所有星期六，星期日不上班。星期一至星期五，按照个人的workshift计算迟到情况。
    对于有特殊作息的日期，按照特殊日期计算迟到情况。
    """
    date = models.CharField(u'日期', db_column='date', max_length=50, blank=True)
    workshift = models.CharField(u'作息时间', db_column='workshift', max_length=100, blank=True)
    is_workday = models.BooleanField(verbose_name=u'是否工作日', db_column='is_workday', default=True)
    note=models.TextField(verbose_name=u'备注', db_column='note', blank=True)

    class Meta:
        verbose_name = u'特殊作息日期'
        db_table = 'special_workshift'
